package cn.icatw.leetcode.editor.cn.juc;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * @author 王顺 762188827@qq.com
 * @apiNote
 * @since 2024/8/14
 */
public class CyclicBarrierExample {
    public static void main(String[] args) {
        int cyclicBarrierCount = 3;
        CyclicBarrier cyclicBarrier = new CyclicBarrier(cyclicBarrierCount, () -> {
            System.out.println("所有线程执行完毕");
        });
        for (int i = 0; i < cyclicBarrierCount; i++) {
            new Thread(() -> {
                System.out.println(Thread.currentThread().getName() + "开始执行");
                try {
                    Thread.sleep(1000);
                    cyclicBarrier.await();
                } catch (InterruptedException | BrokenBarrierException e) {
                    e.printStackTrace();

                }
            }).start();
        }
    }
}
